﻿using IceCS.EMS.BLL;
using IceCS.EMS.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IceCS.EMS.UI.WinFormCore
{
    public partial class FormEdit : Form
    {
        public int EditEmployeeID { get; set; }
        public FormEdit()
        {
            InitializeComponent();
        }

        private void FrmAdd_Load(object sender, EventArgs e)
        {
            var emp = FormService.EmployeeManager.SearchById(EditEmployeeID);
            textBoxEmployeeID.Text = EditEmployeeID.ToString();
            textBoxLastName.Text = emp.LastName;
            textBoxFirstName.Text = emp.FirstName;
            textBoxPosition.Text = emp.Position;
            dateTimePickerBirthday.Value = emp.Birthday;
            textBoxCountry.Text = emp.Country;
            textBoxCity.Text = emp.City;
            textBoxAddress.Text = emp.Address;
            textBoxRemark.Text = emp.Remark;
        }

        public void SetEditId(int employeeID)
        {
            EditEmployeeID = employeeID;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (!this.ValidateChildren())
            {
                return;
            }
            //获取雇员编号显示出来 
            var empId = FormService.EmployeeManager.GetId();
            EmployeeModel newModel = new EmployeeModel
            {
                EmployeeID = EditEmployeeID,
                FirstName = textBoxFirstName.Text,
                LastName = textBoxLastName.Text,
                Country = textBoxCountry.Text,
                City = textBoxCity.Text,
                Address = textBoxAddress.Text,
                Position = textBoxPosition.Text,
                Birthday = dateTimePickerBirthday.Value,
                CreateTime = DateTime.Now,
                CreatorId = AppInfo.CurrentUser.EmployeeID,
                UpdateTime = DateTime.Now,
                UpdateId = AppInfo.CurrentUser.EmployeeID,
                Remark = textBoxRemark.Text
            };

            //保存修改
            if (FormService.EmployeeManager.SaveEdit(newModel))
            {
                MessageBox.Show("修改成功");
                FormService.FormMain.ReloadGridData();
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
